package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.UserAccountsDao;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.dao.model.UserAccountsDetail;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class UserAccountsHandlerController extends AbstractUIController
		implements Initializable {

	@FXML
	Parent root;
	@FXML
	private Button Add;
	@FXML
	private Button delete;
	@FXML
	private GridPane grid;
	@FXML
	private Button modify;
	@FXML
	private TableView<UserAccountsDetail> displayTable;
	@FXML
	private TableColumn<UserAccountsDetail, String> userName;
	@FXML
	private TableColumn<UserAccountsDetail, String> activeTill;
	@FXML
	private TableColumn<UserAccountsDetail, String> createdBy;
	@FXML
	private TableColumn<UserAccountsDetail, String> createionDate;
	
	UserAccountsDao userAccountsDao = PayrollApplicationFactory.getUserAccountsDao();
	
	//TODO:
	UserAccounts logedInUser = null;
	Company company = null;
	UserAccounts currentUser = null;

	public void add(ActionEvent event) {

		System.out.println("add user");
		ManageUserAccountsControler.setAdd(true);
		PayrollApplicationFactory.getMainController().showScreen("ManageUserAccounts.fxml", true, company,logedInUser);
	
	}

	public void delete(ActionEvent event) {
		
		
		UserAccountsDetail userDetail = null;
		if(displayTable.getSelectionModel().getSelectedIndex() < 0){
			showError(UIMessages.SELECT_USER_ACC_TO_DELETE);
			return;
		}
		userDetail = displayTable.getSelectionModel().getSelectedItem();
		try {
			
			if(showConfirmDialog(UIMessages.CONFIRM_DELETE)){
				userAccountsDao.deleteUserAccount(userDetail.getUserName());
				refreshUI(company,logedInUser);
			
			}
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
		}
		
		return;
	}

	public void modify() {
		
		System.out.println("Modify user");
		
		
		UserAccountsDetail userDetail = null;
		if(displayTable.getSelectionModel().getSelectedIndex() < 0){
			showError(UIMessages.SELECT_USER_ACC_TO_DELETE);
			return;
		}
		if(!showConfirmDialog(UIMessages.CONFIRM_MODIFY)){
			return;
		}
		userDetail = displayTable.getSelectionModel().getSelectedItem();
		
		try {
			currentUser = userAccountsDao.getUserAccountsByUserName(userDetail.getUserName());
			if(currentUser == null){
				return;
			}
			
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
		}
		System.out.println("add user");
		ManageUserAccountsControler.setAdd(false);
		if(!currentUser.isActive()){
			
			if(!showConfirmDialog(String.format(UIMessages.ACCOUNT_INACTIVE,currentUser.getUsername()))){
				
				return;
			}
		}
		
		PayrollApplicationFactory.getMainController().showScreen("ManageUserAccounts.fxml", true, company,logedInUser,currentUser);
	}
	
	
	public void goBack() {

		PayrollApplicationFactory.getMainController().showScreen(
				MainController.COMPANIES_LIST_SCREEN, false, logedInUser);
	}


	public void reset(ActionEvent event) {
		
		refreshUI(company,logedInUser);
	
	}

	@Override
	public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
		
		iniTableProps();
	}

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return UIMessages.UITitles.USER_ACCOUNTS;
	}

	@Override
	public void refreshUI(Object... args) {
		
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;
			
			if(args.length > 1 && args[1] instanceof UserAccounts){
				logedInUser = (UserAccounts) args[1];
			}
		}
		populateUserAccountsTable(logedInUser.getUsername());
	}

	private void populateUserAccountsTable(String userName) {
		
		displayTable.getItems().clear();
		ObservableList<UserAccountsDetail> data = FXCollections.observableArrayList();
		
		PayRollUtility.debug("===================== populate user accounts start =====================");

		data.clear();
		try {
			ArrayList<UserAccounts> accList = userAccountsDao.getUserAccountsByParent(logedInUser.getUsername());
			System.out.println("accList : " + accList);
			if(accList == null){
				return;
			}
			data.addAll(UserAccountsDetail.getUserAccDetailList(accList));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		PayRollUtility.debug("data  : " + data);
		displayTable.setItems(data);

		PayRollUtility.debug("===================== populate user accounts end =====================");

	}

	private void iniTableProps() {
		displayTable.setEditable(false);

		userName.setCellValueFactory(new PropertyValueFactory<UserAccountsDetail, String>(
				"userName"));
		createdBy.setCellValueFactory(new PropertyValueFactory<UserAccountsDetail, String>(
						"createdBy"));
		createionDate.setCellValueFactory(new PropertyValueFactory<UserAccountsDetail, String>(
						"createionDate"));
		activeTill.setCellValueFactory(new PropertyValueFactory<UserAccountsDetail, String>(
				"activeTill"));

	}
}
